cmake_minimum_required(VERSION 3.10.2)

###########################################################
#### Project configuration                             ####
###########################################################

project(MMMSegmentation)

###########################################################
#### Source configuration                              ####
###########################################################

set(SOURCE_FILES
    MotionSegmenter.h
    MotionSegmenterFactory.h
    Segmentation.h
)

set(HEADER_FILES
    Segmentation.cpp
)

###########################################################
#### CMake package configuration                       ####
###########################################################

find_package(MMMCore REQUIRED)
set(EXTERNAL_LIBRARY_DIRS ${EXTERNAL_LIBRARY_DIRS} MMMCore)

find_package(Simox REQUIRED)
set(EXTERNAL_LIBRARY_DIRS ${EXTERNAL_LIBRARY_DIRS} VirtualRobot)

set(EXTERNAL_LIBRARY_DIRS ${EXTERNAL_LIBRARY_DIRS} MMMSimoxTools)

###########################################################
#### Project build configuration                       ####
###########################################################

include_directories(${EXTERNAL_INCLUDE_DIRS})
add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${HEADER_FILES})
target_link_libraries(${PROJECT_NAME} PUBLIC ${EXTERNAL_LIBRARY_DIRS} dl)

# this allows dependant targets to automatically add include-dirs by simply linking against this project
target_include_directories(${PROJECT_NAME} PUBLIC
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/..> # TODO: other code requires headers like "#include <MMMSegmentation/xyz.h>" so we need
    $<INSTALL_INTERFACE:include>                      # TODO: the parent dir for BUILD_INTERFACE. find a more elegant way i guess?
)

install(
    TARGETS ${PROJECT_NAME}
    EXPORT "${CMAKE_PROJECT_NAME}Targets"
    LIBRARY DESTINATION lib
    #ARCHIVE DESTINATION lib
    #RUNTIME DESTINATION bin
    #INCLUDES DESTINATION include # seems unnecessary because of target_include_directories()
    COMPONENT lib
)

install(FILES ${HEADER_FILES} DESTINATION "include/${PROJECT_NAME}" COMPONENT dev)

add_definitions(-DMMMTools_LIB_DIR="${MMMTools_LIB_DIR}")
add_definitions(-DMMMTools_SRC_DIR="${MMMTools_SOURCE_DIR}")
add_definitions(-DMOTION_SEGMENTER_PLUGIN_LIB_DIR="${MOTION_SEGMENTER_PLUGIN_LIB_DIR}")

###########################################################
#### Compiler configuration                            ####
###########################################################

include_directories(${EXTERNAL_INCLUDE_DIRS})
link_directories(${EXTERNAL_LIBRARY_DIRS})
add_definitions(${EXTERNAL_LIBRARY_FLAGS})

###########################################################
#### Motion Segmenter Plugins                          ####
###########################################################

# Add motion segmenter plugins
function(DefineMotionSegmenterPlugin MotionSegmenterPlugin_Name MotionSegmenterPlugin_Sources MotionSegmenterPlugin_Headers MotionSegmenterPlugin_Moc MotionSegmenterPlugin_Ui SensorPlugin_Name)
    VirtualRobotQtLibrary(${MotionSegmenterPlugin_Name} "${MotionSegmenterPlugin_Sources}" "${MotionSegmenterPlugin_Headers}" "${MotionSegmenterPlugin_Moc}" "${MotionSegmenterPlugin_Ui}")
    target_link_libraries(${MotionSegmenterPlugin_Name} PUBLIC ${EXTERNAL_LIBRARY_DIRS} ${SensorPlugin_Name})
    set_target_properties(${MotionSegmenterPlugin_Name} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${MOTION_SEGMENTER_PLUGIN_LIB_DIR})

    install(
        TARGETS ${MotionSegmenterPlugin_Name}
        EXPORT "${CMAKE_PROJECT_NAME}Targets"
        LIBRARY DESTINATION lib/mmm_plugins/motion_segmentation
        #ARCHIVE DESTINATION lib
        #RUNTIME DESTINATION bin
        #INCLUDES DESTINATION include
        COMPONENT lib
    )
    install(FILES ${MotionSegmenterPlugin_Headers} DESTINATION "include/MMM/${MotionSegmenterPlugin_Name}" COMPONENT dev)
endfunction()

add_subdirectory(MotionSegmenterPlugin)

###########################################################
#### Motion Handler Plugin                             ####
###########################################################

add_subdirectory(MotionHandlerPlugin)
